"""
视图及逻辑处理工具

"""
from flask import jsonify

# 1开头的一般为用户模块
code_map = {
    10000: '成功',
    10001: "短信发送失败",
    10002: "请勿重复发送验证码",
    10003: "验证码过期",
    10004: "密码错误",
    100015: "原密码错误",
    10005: "系统错误",
    10006: "没有权限",
    10007: "token过期请重新登录",
    10008: "记录未找到",
    10009: "参数不合法",
    10010: "Mqtt连接失败",
    10011: "未登录",
    10012: "文件未找到",
    10013: "超时",
    10014: "回调失败",
    10016: "接口地址未找到",
}


def resp_with_data(code=None, data=None, msg=None):
    if code != 10000:
        msg = code_map[code]
    if type(data) == dict and 'code' in data.keys() and data['code'] != 10000:
        response = jsonify({'code': data['code'], 'message': data["message"], 'data': data["data"]})
    elif type(msg) == dict:
        response = jsonify({'code': msg['code'], 'message': msg["message"], 'data': msg["data"]})
    else:
        response = jsonify({'code': code, 'message': msg, 'data': data})
    return response
